# -*- coding: utf-8 -*-
# created on 2017/04/12

from sympy import sympify
from mathsolver.functions.base import BaseFunction, BaseIneq, BaseAny, BaseEq, new_latex
from mathsolver.functions.hanshu.helper import check_func
from mathsolver.functions.hanshu.hengchengli import HengChengLiRenYiXing


class ZuiXiaoZhiQiuCan(BaseFunction):
    """函数在某处取 最小值 求参数"""
    symbol = '>='

    def solver(self, *args):
        func = check_func(args[0])
        expr, var = func.expression, func.var

        # 在 x = 0处取得最值，在 0 处取得最值
        arg1 = args[1]
        if isinstance(arg1, BaseAny):
            m = arg1.sympify()
        elif isinstance(arg1, BaseEq):
            m = sympify(arg1.value[1])
        else:
            raise ValueError("args[1] must be BaseAny or BaseEq")

        ineq = [expr, self.symbol, expr.subs(var, m)]

        self.steps.append(["", "由题意知 %s %s %s 恒成立" % (new_latex(ineq[0]), ineq[1], new_latex(ineq[1]))])

        if len(args) > 2:
            hcl_solver = HengChengLiRenYiXing(known=self.known).solver(BaseIneq(ineq), args[2])
        else:
            hcl_solver = HengChengLiRenYiXing(known=self.known).solver(BaseIneq(ineq))

        self.steps.extend(hcl_solver.steps)
        self.output = hcl_solver.output
        self.label.add("函数在某处取最值求参数")
        return self


class ZuiDaZhiQiuCan(ZuiXiaoZhiQiuCan):
    """函数在某处取 最大值 求参数"""
    symbol = '<='


if __name__ == '__main__':
    pass
